home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / smaltalk / gnu_st.lha / gnu_st / smalltalk-1.1.1 / OrderedCollection.st < prev    next >
Text File  |  1991-09-12  |  8KB  |  264 lines

  1. "======================================================================
  2. |
  3. |   OrderedCollection Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbyrne     19 Sep 89      Converted to use real method categories.
  34. |
  35. | sbyrne     25 Apr 89      created.
  36. |
  37. "
  38.  
  39. SequenceableCollection variableSubclass: #OrderedCollection
  40.                instanceVariableNames: 'firstIndex lastIndex'
  41.                classVariableNames: ''
  42.                poolDictionaries: ''
  43.                category: nil !
  44.  
  45. OrderedCollection comment: 
  46. 'My instances represent ordered collections of arbitrary typed objects which
  47. are not directly accessible by an index.  They can be accessed indirectly
  48. through an index, and can be manipulated by adding to the end or based
  49. on content (such as add:after:)' !
  50.  
  51. !OrderedCollection class methodsFor: 'instance creation'!
  52.  
  53. new: anInteger
  54.     ^(super new: anInteger) initIndices
  55. !
  56.  
  57. new
  58.     ^self new: 16
  59.  
  60. !!
  61.  
  62.  
  63.  
  64. !OrderedCollection methodsFor: 'accessing'!
  65. at: index
  66.     index _ index + firstIndex - 1.
  67.     (index >= firstIndex and: [ index <= lastIndex ])
  68.         ifTrue: [ ^self basicAt: index ]
  69.     ifFalse: [ self error: 'index out of bounds for ordered collection' ]
  70. !
  71.  
  72. at: index put: anObject
  73.     index _ index + firstIndex - 1.
  74.     (index >= firstIndex and: [ index <= lastIndex ])
  75.         ifTrue: [ ^self basicAt: index put: anObject ]
  76.     ifFalse: [ self error: 'index out of bounds for ordered collection' ]
  77. !    
  78.  
  79. after: oldObject
  80.     "Return the element after oldObject.  Error if oldObject not found or
  81.     if no following object is available"
  82.     firstIndex to: lastIndex do:
  83.         [ :index |        "should we use '=' or '==' here?"
  84.         (self basicAt: index) = oldObject
  85.             ifTrue: [
  86.             index < lastIndex
  87.                 ifTrue: [ ^self basicAt: index + 1 ]
  88.             ifFalse: [ ^self error: 'no following object' ] ]
  89.         ].
  90.     self error: 'object not found'
  91. !
  92.  
  93. before: oldObject
  94.     "Return the element after oldObject.  Error if oldObject not found or
  95.     if no following object is available"
  96.     firstIndex to: lastIndex do:
  97.         [ :index |        "should we use '=' or '==' here?"
  98.         (self basicAt: index) = oldObject
  99.             ifTrue: [
  100.             index > firstIndex
  101.                 ifTrue: [ ^self basicAt: index - 1 ]
  102.             ifFalse: [ ^self error: 'no preceding object' ] ]
  103.         ].
  104.     self error: 'object not found'
  105. !
  106.  
  107. copyEmpty
  108.     ^self species new: self basicSize
  109. !
  110.  
  111. size
  112.     ^lastIndex - firstIndex + 1
  113. !!
  114.  
  115.  
  116.  
  117. !OrderedCollection methodsFor: 'adding'!
  118.  
  119. add: anObject
  120.     ^self addLast: anObject
  121. !
  122.  
  123. add: newObject after: oldObject
  124.     firstIndex to: lastIndex do:
  125.         [ :i | (self basicAt: i) = oldObject
  126.                 ifTrue: [ self at: i + 1 insertObject: newObject.
  127.                               ^newObject ] ].
  128.     self error: 'object not found in collection'
  129. !
  130.  
  131. add: newObject before: oldObject
  132.     firstIndex to: lastIndex do:
  133.         [ :i | (self basicAt: i) = oldObject
  134.                 ifTrue: [ self at: i - 1 insertObject: newObject.
  135.                               ^newObject ] ].
  136.     self error: 'object not found in collection'
  137. !
  138.  
  139.  
  140. addAllFirst: anOrderedCollection
  141.     anOrderedCollection reverseDo:
  142.         [ :element | self addFirst: element ].
  143.     ^anOrderedCollection
  144. !
  145.     
  146. addAllLast: anOrderedCollection
  147.     anOrderedCollection do:
  148.         [ :element | self addLast: element ].
  149.     ^anOrderedCollection
  150. !
  151.     
  152.  
  153. addFirst: newObject
  154.     firstIndex = 1
  155.         ifTrue: [ self growFirst ].
  156.     firstIndex _ firstIndex - 1.
  157.     ^self basicAt: firstIndex put: newObject
  158. !
  159.     
  160. addLast: newObject
  161.     lastIndex = self basicSize
  162.         ifTrue: [ self growLast ].
  163.     lastIndex _ lastIndex + 1.
  164.     ^self basicAt: lastIndex put: newObject
  165. !!
  166.  
  167.  
  168.  
  169. !OrderedCollection methodsFor: 'removing'!
  170. removeFirst
  171.     lastIndex < firstIndex
  172.         ifTrue: 
  173.             [ ^self error: 'attempted to remove from an empty collection' ].
  174.     firstIndex _ firstIndex + 1.
  175.     ^self basicAt: firstIndex - 1
  176. !
  177.  
  178. removeLast
  179.     lastIndex < firstIndex
  180.         ifTrue: 
  181.             [ ^self error: 'attempted to remove from an empty collection' ].
  182.     lastIndex _ lastIndex - 1.
  183.     ^self basicAt: lastIndex + 1
  184. !!
  185.  
  186.  
  187.  
  188. !OrderedCollection methodsFor: 'private methods'!
  189.  
  190. initIndices
  191.     firstIndex _ self basicSize // 2 max: 1.
  192.     lastIndex _ firstIndex - 1
  193. !
  194.  
  195. firstIndex: first lastIndex: last
  196.     firstIndex _ first.
  197.     lastIndex _ last
  198. !
  199.  
  200. growFirst
  201.     "Make growSize room at the start of the ordered collection, and copy
  202.     all the elements of the old collection into the new one starting
  203.     at the value that growSize returned."
  204.     | newOrderedCollection delta |
  205.     delta _ self growSize.
  206.     newOrderedCollection _ self growTo: self basicSize + delta.
  207.     firstIndex to: lastIndex do:
  208.         [ :index | newOrderedCollection basicAt: delta + index - 1
  209.                                     put: (self basicAt: index) ].
  210.     newOrderedCollection firstIndex: delta + firstIndex - 1
  211.                          lastIndex: delta + lastIndex - 1.
  212.     ^self become: newOrderedCollection    
  213. !
  214.  
  215. growLast
  216.     "Make growSize room at the end of the ordered collection, and copy
  217.     all the elements of the old collection into the new one starting
  218.     at firstIndex."
  219.     | newOrderedCollection |
  220.     newOrderedCollection _ self growTo: self basicSize + self growSize.
  221.     firstIndex to: lastIndex do:
  222.         [ :index | newOrderedCollection basicAt: index
  223.                                     put: (self basicAt: index) ].
  224.     newOrderedCollection firstIndex: firstIndex
  225.                          lastIndex: lastIndex.
  226.     ^self become: newOrderedCollection    
  227. !
  228.  
  229. grow
  230.     "Make growSize room in the collection, putting the old contents in the
  231.     middle."
  232.     | oldSize newSize newFirstIndex newOrderedCollection |
  233.     oldSize _ self basicSize.
  234.     newSize _ oldSize + self growSize.
  235.     newOrderedCollection _ self growTo: newSize.
  236.     newFirstIndex _ newSize - oldSize // 2 max: 1.
  237.     1 to: self size do:
  238.         [ :i | newOrderedCollection basicAt: i + newFirstIndex - 1
  239.                                 put: (self basicAt: i + firstIndex - 1) ].
  240.     newOrderedCollection firstIndex: newFirstIndex
  241.                          lastIndex: newFirstIndex + self size - 1.
  242.     ^self become: newOrderedCollection
  243. !
  244.  
  245. growTo: anInteger
  246.     ^self species new: anInteger
  247. !    
  248.  
  249. growSize
  250.     ^10                "number out of a hat"
  251. !
  252.  
  253. at: index insertObject: anObject
  254.     lastIndex = self basicSize ifTrue: [ self growLast ].
  255.     lastIndex to: index by: -1 do:
  256.         [ :i | self basicAt: i + 1
  257.                 put: (self basicAt: i) ].
  258.     self basicAt: index put: anObject.
  259.     ^anObject
  260. !!
  261.  
  262.